<?php
#MySQLi开源库的php的二次封装
class dbUtils
{
    var $respone_body = array
    (
        'code'  =>false,
        'msg'   =>"",
        'debug' =>"",
    );
    /**
     * @method insert_row 插入一行
     * @param MysqliDb $db 开源类
     * @param string $repeat_col 设置要检测的重复的字段
     * @param string $repeat_var 传入的字段
     * @param string $table 数据表
     * @param array $data 要插入的数组：例：列表名=>数据名
     * @param bool $repeat 是否允许插入重复的数据
     * @return void 空
     */
    function insert_row($db,$repeat_col,$repeat_var,$table,$data,$repeat)
    {
        if($repeat)
        {
            $id = $db->insert($table,$data);
            if($id)
            {
                $this->respone_body['code'] = true;
                $this->respone_body['msg']  = "插入成功".$id;
                echo json_encode($this->respone_body);
                
            }
            else
            {
                $this->respone_body['code']  = false;
                $this->respone_body['msg']   = "插入失败".$db->getLastError();
                $this->respone_body['debug'] = $db->getLastError(); 
                echo json_encode($this->respone_body);
            }
        }
        else
        {
            #SELECT task_name from collect WHERE task_name = $repeat_var
            $db->where($repeat_col,$repeat_var);
            $result = $db->getValue($table,$repeat_col,null);
            $count = $db->count;
            #var_dump($result);
            #echo $count;
            if($count > 0)
            {
                $this->respone_body['code']  = false;
                $this->respone_body['msg']   = "数据重复！";
                $this->respone_body['debug'] = $result; 
                echo json_encode($this->respone_body);
            }
            else if($count == 0)
            {
                $id = $db->insert($table,$data);
                $this->respone_body['debug'] = "插入结果：".$id; 
                $this->respone_body['code']  = true;
                $this->respone_body['msg']   = "count为0，数据不重复，可以插入"; 
                echo json_encode($this->respone_body);
            }
        }
    }
    /**
     * @return json 数据表数据
     */
    function get_all($db,$table)
    {
        #$data = $db->get($table,null);
        $json = $db->JsonBuilder()->get($table,null);
        echo $json;
    }
    /**
     * 获取一行 第一行
     * @return data Object数据
     */
    function get_row($db,$table)
    {
        $db->where("a","A");
        $data = $db->ObjectBuilder()->getOne($table);
        echo $data;
    }
}
?>